package com.atguigu.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.executor.MyExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


/**
 * Created with IntelliJ IDEA2020.
 * User: yangchao
 * Date: 2021/11/20:16:45
 * Description:
 */
@Controller
public class WebSkuDetailController {
    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private SearchFeignClient searchFeignClient;
    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        Map<String, Object> dataMap = new HashMap<>();
        //1.根据skuid获取sku的基本信息(skuInfo)
        CompletableFuture<SkuInfo> skuFuture = CompletableFuture.supplyAsync(()->{
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            dataMap.put("skuInfo",skuInfo);
            return skuInfo;
        });
        //2.根据skuid获取spu的销售属性
        CompletableFuture<Void> spuSalePropertyFuture = skuFuture.thenAcceptAsync(skuInfo -> {
            //销售属性
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSpuSaleProperty(skuInfo.getProductId(), skuId);
            dataMap.put("spuSalePropertyList", spuSalePropertyList);
        }, MyExecutor.getInstance());


        //3.根据skuid获取sku的分类信息
        CompletableFuture<Void> categoryViewFuture = skuFuture.thenAcceptAsync(skuInfo -> {
            //分类信息
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            dataMap.put("categoryView", categoryView);
        }, MyExecutor.getInstance());


        //4.根据skuid获取sku的实时价格信息
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(()->{
            BigDecimal price = productFeignClient.getSkuPrice(skuId);
            dataMap.put("price",price);
        });
        //5.根据spuId获取该spu所对应的所有sku的销售属性值与sku的对应关系
        CompletableFuture<Void> salePropertyValueIdJsonFuture = skuFuture.thenAcceptAsync(skuInfo -> {
            //获取销售属性与sku的关系
            Map salePropertyValueIdMap = productFeignClient.getSkuSalePropertyValueIdMapping(skuInfo.getProductId());
            dataMap.put("salePropertyValueIdJson", JSON.toJSONString(salePropertyValueIdMap));
        }, MyExecutor.getInstance());

        //更新商品热度
//        CompletableFuture<Void> hotScoreFuture = CompletableFuture.runAsync(() -> {
//            searchFeignClient.incrHotScore(skuId);
//        }, MyExecutor.getInstance());
//        CompletableFuture<Void> hotScoreFuture = skuFuture.thenAcceptAsync(skuInfo -> {
//            if(skuInfo!=null){
//                searchFeignClient.incrHotScore(skuInfo.getId());
//            }
//        }, MyExecutor.getInstance());


        //多任务组合 必须这五个全部执行完拿到数据之后才走 赋值给页面
        CompletableFuture.allOf(
                skuFuture,
                spuSalePropertyFuture,
                categoryViewFuture,
                priceFuture,
                salePropertyValueIdJsonFuture
//                , hotScoreFuture
        ).join();

        model.addAllAttributes(dataMap);
        return "detail/index";
    }
}


